package com.atguigu.tingshu;

import com.atguigu.tingshu.album.config.MinioConstantProperties;
import io.minio.MinioClient;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.time.Duration;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableScheduling
public class ServiceAlbumApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceAlbumApplication.class, args);
    }

    @Autowired
    MinioConstantProperties minioConstantProperties;

    /**
     * 初始化客户端对象
     * @return
     */
    @Bean
    public MinioClient getClient(){
        return MinioClient.builder()
                .endpoint(minioConstantProperties.getEndpointUrl())
                .credentials(minioConstantProperties.getAccessKey()
                        ,minioConstantProperties.getSecreKey()).build();
    }


    @Autowired
    RedissonClient redissonClient;

    /**
     * 布隆过滤器初始化
     * @return
     */
    @Bean
    public RBloomFilter initRBloomFilter(){
        // 初始化过滤器并命名
        RBloomFilter<Object> albumBloomFilter =
                redissonClient.getBloomFilter("album_bloom_filter");
        // 设置过滤器 数据体积 和 错误差范围
        albumBloomFilter.tryInit(200000,0.01);
        // 设置过滤器过期时间
//        albumBloomFilter.expire(Duration.ofDays(1));
        // 返回
        return albumBloomFilter;
    }

}
